﻿using AixRegisterInfodll;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using FinalTestMachine.Motion.Aixs;
using FinalTestMachine.Motion.Process;
using FinalTestMachine.Motion.Aixs.Light;

namespace FinalTestMachine.Motion.IO
{
    /// <summary>
    /// 静态的IO 额外扫描
    /// 在这里可以添加你所想要的IO状态扫描
    /// </summary>
    public static class IOScanHelper
    {
        static IOScanHelper()
        {
            Connect.ConnectFactory.TCP_ScanCode.RecevieMsgActionEventHander += SerialPort_ScanCode_RecevieMsgActionEventHander;
        }

        private static void SerialPort_ScanCode_RecevieMsgActionEventHander(string obj)
        {
            StaticIOHelper.Out_LoadScanTrigger.Out_OFF();
        }

        private static RM_dll2.MotionHelper motion => StaticInitial.Motion;

        private static RM_dll2.ExtraMsgHelper msgHelper = new RM_dll2.ExtraMsgHelper();

        private static IoCheck ioCheck_ScanCode = new IoCheck(StaticIOHelper.In_LoadScannerTriggerChecked.In);

        /// <summary>
        /// 双启动信号检测
        /// </summary>
        public static bool IsLoadStart = false;

        /// <summary>
        /// NG料检测
        /// </summary>
        public static bool NGisPut = false;

        private static IoCheck ioCheck_IsTesting = new IoCheck(() => SecondTestStation.Cur.IsRunning || ThirdTestStation.Cur.IsRunning || FourTestStation.Cur.IsRunning);
        private static IoCheck ioCheck_Start = new IoCheck(StaticIOHelper.CheckIn_Start);
        private static IoCheck ioCheck_SafeEMG = new IoCheck(() => StaticIOHelper.In_EMG.In());

        /// <summary>
        /// 扫描方法
        /// </summary>
        public static void Scan()
        {
            if (ioCheck_Start.RisingCheck())
            {
                if (StaticIOHelper.CheckIn_Start())
                {
                    IsLoadStart = true;
                }
                else
                {
                    IsLoadStart = false;
                }
            }

            msgHelper.Info = "";

            if (Funs.FunCommSelection.Cur.IsUseSafetyGrating)       //判断是否启用安全光栅
            {
                if (!StaticIOHelper.In_Safe_Checked.In())
                {
                    if (motion.CurOrder.IsResing)
                    {
                        motion.CommitStopOrder();
                    }

                    if (Aix_Turn.Cur.IsBusy)
                    {
                        Aix_Turn.Cur.StopSlowDown();
                        motion.WriteErrToUser("光栅触发");
                        System.Windows.MessageBox.Show("光栅触发!", "警告");
                    }

                    if (SecondTestStation.Cur.IsProcessRunning || ThirdTestStation.Cur.IsProcessRunning || FourTestStation.Cur.IsProcessRunning)
                    {
                        motion.WriteErrToUser("光栅触发");
                        System.Windows.MessageBox.Show("光栅触发!", "警告");
                    }

                    StaticIOHelper.BaffleStop();
                    msgHelper.Info = "光栅触发";
                }
            }

            if (Funs.FunCommSelection.Cur.IsUseSafeDoor)
            {
                if (motion.CurOrder.IsAutoRunning)
                {
                    if (!StaticIOHelper.In_LeftDoorChecked.In() || !StaticIOHelper.In_RightDoorChecked.In() || !StaticIOHelper.In_BackDoorChecked.In())
                    {
                        msgHelper.Info = "Attention to safety: Security door open!";
                        if (!StaticIOHelper.In_LeftDoorChecked.In())
                        {
                            motion.WriteErrToUser("自动运行中，请关闭左侧门锁");
                        }
                        if (!StaticIOHelper.In_RightDoorChecked.In())
                        {
                            motion.WriteErrToUser("自动运行中，请关闭右侧门锁");
                        }
                        if (!StaticIOHelper.In_BackDoorChecked.In())
                        {
                            motion.WriteErrToUser("自动运行中，请关闭后侧门锁");
                        }

                    }
                }
            }

            if (Funs.FunCommSelection.Cur.IsUseSafeDoor)
            {
                if ((!StaticIOHelper.In_LeftDoorChecked.In() || !StaticIOHelper.In_RightDoorChecked.In() || !StaticIOHelper.In_BackDoorChecked.In()) && motion.CurOrder.IsAutoRunning)
                {
                    Aixs.Aix_Turn.Cur.StopSlowDown();
                    Aixs.Site6.Aix_Site6_PipeUpDown.Cur.StopSlowDown();
                    Aixs.Site2.Aix_Site2_PlaneSourceLift.Cur.StopSlowDown();
                    Aixs.Site4.Aix_Site4_PlaneSourceLift.Cur.StopSlowDown();
                    Aixs.Site4.Aix_Site4_RelayMirror.Cur.StopSlowDown();
                    Aixs.Site2.Aix_Site2_RelayMirror.Cur.StopSlowDown();
                    Aix_LightBaseBase.Fun_StopAll();

                    throw new Exception("自动运行中检测到安全门开启，停止所有轴！");
                }
            }
            
            if (ioCheck_ScanCode.RisingCheck())
            {
                StaticIOHelper.Out_LoadScanTrigger.Out_ON();
            }
            else
            {
                StaticIOHelper.Out_LoadScanTrigger.Out_OFF();
            }

            /*if (!StaticIOHelper.In_FrontEMG.In())
            {
                motion.WriteErrToUser("前急停被按下！！！！");
            }

            if (!StaticIOHelper.In_BackEMG.In())
            {
                motion.WriteErrToUser("后急停被按下！！！！");
            }*/

            RotatedMovNextHelper.ScanCheckMainPos();

            /*if (!StaticIOHelper.In_NgBoxPutChecked.In())
            {
                NGisPut = true;
            }*/

            if (!motion.CurOrder.IsAutoRunning)
            {
                StaticIOHelper.StartLightOff();
            }
        }
    }
}